import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../controller.dart';

/// 消耗品展示组件
/// 作用：展示用户已购买的消耗品，提供消耗操作入口
class ConsumableBox extends GetView<PaymentController> {
  const ConsumableBox({super.key, required this.controller});

  final PaymentController controller;

  @override
  Widget build(BuildContext context) {
    // 加载中状态
    if (controller.state.loading.value) {
      return const Card(
          child: ListTile(
              leading: CircularProgressIndicator(),
              title: Text('Fetching consumables...')));
    }

    // 内购服务不可用或消耗品ID未找到时不展示
    if (!controller.state.isAvailable.value || 
        controller.state.notFoundIds.contains('consumable')) {
      return const Card();
    }

    // 消耗品列表头部
    const ListTile consumableHeader =
        ListTile(title: Text('Purchased consumables'));

    // 构建消耗品图标列表
    final List<Widget> tokens = controller.state.consumables.map((String id) {
      return GridTile(
        child: IconButton(
          icon: const Icon(
            Icons.stars,
            size: 42.0,
            color: Colors.orange,
          ),
          splashColor: Colors.yellowAccent,
          onPressed: () => controller.consume(id), // 消耗操作
        ),
      );
    }).toList();

    return Card(
        child: Column(children: <Widget>[
      consumableHeader,
      const Divider(),
      GridView.count(
        crossAxisCount: 5, // 每行显示5个消耗品图标
        shrinkWrap: true, // 适应内容大小
        padding: const EdgeInsets.all(16.0),
        children: tokens,
      )
    ]));
  }
}
    